const throttle = (fn, delay = 500) => {
  let lastTime = 0;
  return function () {
    let nowTime = new Date();
    if (nowTime - lastTime > delay) {
      lastTime = nowTime;
      fn.apply(this, arguments);
    }
  };
};

const loadingMiddleware = {
  before(vm) {
    vm.setData({
      loadingText: '正在加载',
    });
  },
  after(ret,vm, pageSize = 10,) {
    if (!ret.data.list || ret.data.list.length<=0 || ret.data.list.length<pageSize) {
      vm.setData({
        loadingText: '—— 到底啦 ——',
        canPullUp: false,
      });
    } else {
      vm.setData({
        loadingText: '下拉加载',
        canPullUp: true,
      });
    }
  },
};

const handleReplaceComma = (number,checkEmpty = true) =>  {
  if(typeof number ==='null' || typeof number === 'unfined') {
    return '';
  }

  if(checkEmpty && number.toString().replace(/(^\s*)|(\s*$)/g, '').length === 0){
    return '';
  }

  return Number(String(number).replace(/,/g, ''));
}



module.exports = {
  loadingMiddleware,
  throttle,
  handleReplaceComma
};
